<template>
  <div class="app-container">
    <el-form ref="form" inline :model="form" :rules="rules" label-width="120px">
      <el-form-item label="心跳地址" prop="heartbeatAddress">
        <el-input clearable v-model="form.heartbeatAddress" placeholder="DM8400"/>
      </el-form-item>
      <el-form-item label="条码信号地址" prop="barcodeSignal">
        <el-input clearable v-model="form.barcodeSignal" placeholder="DM8190"/>
      </el-form-item>
      <el-form-item label="条码地址" prop="barcodeAddress">
        <el-input clearable v-model="form.barcodeAddress" placeholder="DM8100"/>
      </el-form-item>
      <el-form-item label="追溯结果地址" prop="traceabilityAddress">
        <el-input clearable v-model="form.traceabilityAddress" placeholder="DM8191"/>
      </el-form-item>
      <el-form-item label="加工结果地址" prop="processingResult">
        <el-input clearable v-model="form.processingResult" placeholder="DM8194"/>
      </el-form-item>
      <el-form-item label="产量地址" prop="productionAddress">
        <el-input clearable v-model="form.productionAddress" placeholder="DM8066" />
      </el-form-item>
      <el-form-item label="设备状态地址" prop="deviceStatusAddress">
        <el-input clearable v-model="form.deviceStatusAddress" placeholder="DM8040" />
      </el-form-item>
      <el-form-item label="模号写入地址" prop="moldNoWriteAddress">
        <el-input v-model="form.moldNoWriteAddress" placeholder="DM8062" />
      </el-form-item>
      <el-form-item label="模号读取地址" prop="moldNoReadAddress">
        <el-input v-model="form.moldNoReadAddress" placeholder="DM8060" />
      </el-form-item>
      <el-form-item label="产量通知地址" prop="productionNotificationAddress">
        <el-input v-model="form.productionNotificationAddress" placeholder="DM8064" />
      </el-form-item>

      <el-form-item label="样件地址" prop="sampleAddres">
        <el-input v-model="form.sampleAddres" placeholder="DM8196" />
      </el-form-item>
      <el-form-item label="节拍地址" prop="beatAddress">
        <el-input v-model="form.beatAddress" placeholder="DM8070" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm" v-if="list.length == 0">确 定</el-button>
        <el-button type="primary" @click="submitForm" v-if="list.length > 0">修 改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  listProcessAddress,
  getProcessAddress,
  delProcessAddress,
  addProcessAddress,
  updateProcessAddress
} from "@/api/mes/process_address";

export default {
  name: "ProcessAddress",
  props: {
    device: {
      type: Object,
      default: null
    }
  },
  watch: {
    device(n, o) {
      this.init()
      this.getList()
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程地址表格数据
      addressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null
      },
      list:[],
      // 表单参数
      form:{},
      // form: {
      //   heartbeatAddress:'DM8400',
      //   barcodeSignal:'DM8190',
      //   barcodeAddress:'DM8100',
      //   traceabilityAddress:'DM8191',
      //   processingResult:'DM8194',
      //   productionAddress:'DM8066',
      //   deviceStatusAddress:'DM8040',
      //   moldNoWriteAddress:'DM8062',
      //   moldNoReadAddress:'DM8060',
      //   productionNotificationAddress:'DM8064',
      // },
      // 表单校验
      rules: {
        // heartbeatAddress: [
        //   {required: true, message: "心跳地址不能为空", trigger: "blur"}
        // ],
        // barcodeSignal: [
        //   {required: true, message: "条码信号不能为空", trigger: "blur"}
        // ],
        // barcodeAddress: [
        //   {required: true, message: "条码地址不能为空", trigger: "blur"}
        // ],
        // barcodeValue: [
        //   {required: true, message: "条码值不能为空", trigger: "blur"}
        // ],
        // traceabilityAddress: [
        //   {required: true, message: "条码追溯地址不能为空", trigger: "blur"}
        // ],
        // processingResult: [
        //   {required: true, message: "加工结果地址不能为空", trigger: "blur"}
        // ]
      }
    };
  },
  methods: {
    init() {
      this.form.deviceId = this.device.id
      this.queryParams.deviceId = this.device.id
    },
    /** 查询流程地址列表 */
    getList() {
      this.loading = true;
      listProcessAddress(this.queryParams).then(response => {
        this.loadProcessAddress(response.rows)
        this.addressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    loadProcessAddress(rows) {
      this.list = rows
      if (!rows || rows.length == 0) {
        return
      }
      this.form = rows[0]
      
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加流程地址";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getProcessAddress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程地址";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProcessAddress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcessAddress(this.form).then(response => {
              this.$modal.msgSuccess("配置成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除流程地址编号为"' + ids + '"的数据项？').then(function () {
        return delProcessAddress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/process/address/export', {
        ...this.queryParams
      }, `address_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
